package com.chen10.gobang.controller;

import com.chen10.gobang.common.enums.response.SuccessMsgEnum;
import com.chen10.gobang.common.exception.BusinessException;
import com.chen10.gobang.common.protocal.ResponseResult;
import com.chen10.gobang.common.protocal.ResponseVO;
import com.chen10.gobang.model.dto.MatchDTO;
import com.chen10.gobang.service.MatchFightService;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/api/matchFight")
public class MatchFightController {

    @Resource
    private MatchFightService matchFightService;

    /**
     * 开始匹配
     * @param matchDTO
     * @return
     */
    @PostMapping("/match")
    public ResponseVO<String> match(@RequestBody MatchDTO matchDTO) {
        try{
            return ResponseResult.success(matchFightService.match(matchDTO));
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(),e.getMessage());
        }
    }


    /**
     * 取消匹配
     * @param matchDTO
     * @return
     */
    @PostMapping("/cancelMatch")
    public ResponseVO<String> cancel(@RequestBody MatchDTO matchDTO) {
        try{
            return ResponseResult.success(matchFightService.cancelMatch(matchDTO));
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(),e.getMessage());
        }

    }

    /**
     * 匹配成功
     * @param matchData
     * @return
     */
    @PostMapping("/matchSuccess")
    public ResponseVO matchSuccess(@RequestBody MultiValueMap<String,String> matchData) {
        System.out.println(matchData);
        try{
            matchFightService.matchSuccess(matchData);
            return ResponseResult.success(SuccessMsgEnum.MATCH_SUCCESS);
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(),e.getMessage());
        }
    }
}
